module anima
import StdEnv, japi

/* This example shows how to create animations, and
animation based games. Sprites must have a transparent
background, that will merge with the environment. In
the example, one must draw the eagle on a transparent
background. There are special image editors that create
a transparent background. Since they are expensive, I use
the free Gimp image processor. The first step is to choose
a background color. Tradition has it that the blue color
is the best one for this kind of task. Then, one must
draw the eagle on a pure blue background, and save it
in the jpg format. From GIMP, choose the 

<Layer/transparency/alpha-color>

option from the task menu. On the popup dialog, choose
the blue color, that will become transparent right away.
Save the image as *.gif. NB: The inicial image must be 
in the JPG format, but the final image must be in the 
GIF format. The reason for this strange mode d'emploi 
is that GIMP detects the transparent color of JPG images 
automatically, but requires the use of a selection mechanism 
for GIF images. Therefore, it is easier to start with a JPG 
image, create  the transparent background, and save the image 
as *.gif. In order to create a GIF image, all you need to do 
is to choose the option <File/Save as>, and change the
extension of the image from *.jpg to *.gif.

BTW, you can obtain GIMP from the Internet, both for
Windows, and for Linux. 

The example also shows how to cut an image. I have cut
a rectangle from the image thus:

ifrogs= jcutimage cv (50, 70) 300 400

The upper left hand corner of the rectangle is at (50, 70),
its width is 300, and its height is 400. The image rectangle
ifrogs will be used to restore the background before
drawing the eagle. I do not perform the restoration
outside the rectangle, so you can see what happens if
one skips this step.
****************/

Start world
  # (js, world) = begin_japi world
    (frm, js) = jnewFrame  (WH 400 500) "Transparent colors" js
    (cv, js) = jnewCanvas (WH 400 500) frm  js
    (frogs, js)= jloadimage "frogs.jpg" js
    cv = jdraw  (Img(100, 120) frogs) cv
    cv = jdraw  (Img(150, 250) frogs) cv
    cv = jdraw (Rct (52,72) 295 395) cv
    ifrogs= jcutimage cv (50, 70) 300 400
    (img_a,js) = load_images js
    (frm, js) = jshow frm js
    js = loop frm 0 (10, 30) cv js with
            loop fr sel (dx, dy) cv js
              | dx > 400
                  # cv= bg_color white cv
                  = loop fr sel (10, 30) cv js
              # (x,js) = jsleep 250 js
              # (na,js) = jgetaction x js
              | na==(act fr)
                  # js= jdispose cv js
                  = jquit js
              #cv = jdraw (Img (50,70) ifrogs) cv
              # cv = jdraw (Img(dx,dy) img_a.[sel]) cv
              # sel = (sel+1) rem (size img_a)
              = loop fr sel (dx+4, dy+5) cv js
  = end_japi js world
where
     load_images :: !JS -> (!{#Int},!*JS)
     load_images js
          # (b1,js) = jloadimage "bird1.gif" js
            (b2,js) = jloadimage "bird2.gif" js
            (b3,js) = jloadimage "bird3.gif" js
            (b4,js) = jloadimage "bird4.gif" js
          =   ({b1, b2, b3, b4},js)

 